package com.cenwangkeji.resident.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.bean.BasicInfoBean;
import com.cenwangkeji.resident.presenter.fragment.MyPresenter;
import com.cenwangkeji.resident.ui.activity.AboutActivity;
import com.cenwangkeji.resident.ui.activity.FeedbackActivity;
import com.cenwangkeji.resident.ui.activity.InfoDetailActivity;
import com.cenwangkeji.resident.ui.activity.MainActivity;
import com.cenwangkeji.resident.ui.activity.MessageActivity;
import com.cenwangkeji.resident.ui.activity.MessageDetailActivity;
import com.cenwangkeji.resident.ui.activity.SettingActivity;

import com.lws.basic.base.BaseActivity;
import com.lws.basic.base.BaseFragment;
import com.lws.basic.utils.common.MyToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @松 on 2019/10/22 16:40
 * 我的个人
 */
public class MyFragment extends BaseFragment implements  MyPresenter.I{
    @BindView(R.id.iv_user)
    ImageView mIvUser;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    Unbinder unbinder;
    private MyPresenter mPresenter = new MyPresenter(this);

    @Override
    public int setLayoutId() {
        return R.layout.fragment_my;
    }


    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        unbinder = ButterKnife.bind(this, rootView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getUserDetail();
    }

    @OnClick({R.id.cl_user, R.id.iv_message, R.id.tv_message, R.id.iv_feedback, R.id.tv_feedback, R.id.iv_about, R.id.tv_about, R.id.iv_setting, R.id.tv_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cl_user://用户
                Intent intent = new Intent(getActivity(), InfoDetailActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_message://消息
            case R.id.tv_message:
                //startActivity(new Intent(getActivity(), MessageDetailActivity.class));
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.iv_feedback://反馈
            case R.id.tv_feedback:
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
                break;
            case R.id.iv_about://关于
            case R.id.tv_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.iv_setting://设置
            case R.id.tv_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }


    @Override
    public void onUserDetail(BasicInfoBean bean) {
        if (bean.getMsg().equals(Constant.token_expired)){
            BaseActivity activity =(MainActivity)getActivity();
            activity.signIn();
        }else {
            if (bean.getData() != null){
                Glide.with(getContext()).load(Constant.base+bean.getData().getPicUrl()).apply(new RequestOptions().circleCrop().error(R.mipmap.ic_user)).into(mIvUser);
                mTvName.setText(bean.getData().getXm());
                mTvPhone.setText(bean.getData().getPhone());
            }else {
                MyToast.show(bean.getMsg());
            }
        }
    }
}
